package com.java110.mall.cart.cmd.storeOrder;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.CmdContextUtils;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrderWriteoff.StoreOrderWriteoffDto;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderWriteoffV1InnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.util.List;

@Java110Cmd(serviceCode = "storeOrder.queryStoreOrderWriteOff")
public class QueryStoreOrderWriteOffCmd extends Cmd {

    @Autowired
    private IStoreOrderWriteoffV1InnerServiceSMO storeOrderWriteoffV1InnerServiceSMOImpl;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        super.validatePageInfo(reqJson);

        Assert.hasKeyAndValue(reqJson,"orderId","未包含订单ID");
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        String userId = CmdContextUtils.getUserId(context);
        StoreOrderDto storeOrderDto = new StoreOrderDto();
        storeOrderDto.setPersonId(userId);
        storeOrderDto.setOrderId(reqJson.getString("orderId"));
        List<StoreOrderDto> storeOrderDtos = storeOrderInnerServiceSMOImpl.queryStoreOrders(storeOrderDto);

        if(ListUtil.isNull(storeOrderDtos)){
            throw new CmdException("未查询到订单");
        }
        StoreOrderWriteoffDto storeOrderWriteoffDto = new StoreOrderWriteoffDto();
        storeOrderWriteoffDto.setOrderId(reqJson.getString("orderId"));
        List<StoreOrderWriteoffDto> storeOrderWriteoffDtos = storeOrderWriteoffV1InnerServiceSMOImpl.queryStoreOrderWriteoffs(storeOrderWriteoffDto);

        context.setResponseEntity(ResultVo.createResponseEntity(storeOrderWriteoffDtos));
    }
}
